// pages/ingredients/ingredients.js
import api from '../../utils/api.js'
var app = getApp();
import { config } from '../../utils/config'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        // 此页面 页面内容距最顶部的距离
        height: app.globalData.height * 2 + 20,
        select: [],
        current: 0,
        ingredients: [],
        numList1: [1, 1, 1, 1],
        numList2: [],
        imgUrl: config.imgUrl,
    },
    toOrder() {
        // var num1 = 0
        // var num2 = 0
        // this.data.numList1.forEach((item)=>{
        //   num1 += item * 1
        // })
        // this.data.numList2.forEach((item) => {
        //   num2 += item * 1
        // })
        // if (num1 == num2){
        //   wx.navigateTo({
        //     url: "/pages/order/order"
        //   })
        // }else{
        //   wx.showToast({
        //     title: '您还未挑选完',
        //     icon: 'none',
        //     duration: 1500
        //   })
        // }
        wx.navigateTo({
            url: "/pages/order/order"
        })

    },
    add(e) {
        var flag = false
        var id = e.target.dataset.id
        var name = e.target.dataset.name
        var select = wx.getStorageSync("select-menus")
        var item = select.filter((item) => {
            return item.id == this.data.goods[this.data.current].id
        })[0]
        item.ingredients.map((item) => {
            if (item.id == id && item.name == name) {
                flag = true
            }
        })
        if (flag) {
            wx.showToast({
                title: '食材已添加过',
                icon: 'none',
                duration: 1500
            })
        } else {
            select.map((item) => {
                if (item.id == this.data.goods[this.data.current].id) {
                    item.ingredients.push({ id, name })
                }
            })
            wx.showToast({
                title: '添加成功',
                icon: 'none',
                duration: 1500
            })
            wx.setStorageSync("select-menus", select)
            var numList1 = this.data.numList1
            numList1[this.data.current]++
                console.log(numList1[this.data.current])
            console.log(numList1)
            this.setData({
                numList1
            })
        }
    },
    changeIndex(e) {
        this.setData({
            current: e.target.dataset.idx
        })
        var ingredients = this.data.goods[e.target.dataset.idx].products
        this.setData({
            ingredients
        })

    },
    del(e) {
        var select = wx.getStorageSync("select-menus");
        select = select.filter((item) => {
            return item.id != e.target.dataset.id
        })
        this.setData({
            select,
            current: 0
        })
        wx.setStorageSync("select-menus", select);
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        //获取已选食材数量
        var select = wx.getStorageSync("select-menus")
        var temp = 0
        for (var i = 0; i < select.length - 1; i++) {
            for (var j = 0; j < select.length - 1 - i; j++) {
                if (select[j].id > select[j + 1].id) {
                    temp = select[j];
                    select[j] = select[j + 1];
                    select[j + 1] = temp;
                }
            }
        }
        var numList1 = []
        select.forEach((item) => {
            numList1.push(item.ingredients.length)
                // numList1.push(0)
        })
        wx.setStorageSync("select-menus", select)
            //获取已选菜谱的列表
        var list = select.concat([])
        var newList = []
        list.forEach((item) => {
            newList.push(item.id)
        })
        var ids = newList.join(",")
        http.request({
            url: api.getProducts,
            method: "POST",
            data: {
                ids
            },
            success: (res) => {
                //获取总菜谱食材的集合
                var goods = res.data
                    //获取第一个菜谱的食材
                var ingredients = res.data[0].products
                    //获取对应食材数量的集合
                var numList2 = []
                goods.forEach((item) => {
                    numList2.push(item.products.length)
                })
                this.setData({
                    numList1,
                    numList2,
                    goods,
                    ingredients
                })
            }
        })


    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {



    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    }
})